package com.atom.module.document.helper;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Point;

import com.atom.module.document.Document;

import java.util.ArrayList;
import java.util.List;

/**
 * All rights Reserved, Designed By www.rongdasoft.com
 *
 * @version V1.0
 * @Title: PointHelper
 * @Description:
 * @author: wuyongzheng
 * @date: 2022/4/1
 * @Copyright: 2022/4/1 www.rongdasoft.com Inc. All rights reserved.
 */
public class PointHelper {

    public static Point[] scalePoints(Matrix matrix, Point[] points) {
        List<Point> result = new ArrayList<>();
        for (Point point : points) {
            result.add(matrix(matrix, point));
        }
        return result.toArray(new Point[]{});
    }

    public static Point[] rotatePoint(Document.Param param, int rotate, Point[] srcArray) {
        return rotatePoint(param.getWidth(), param.getHeight(), rotate, srcArray);
    }

    public static Point[] rotatePoint(int bitmapW, int bitmapH, int rotate, Point[] srcArray) {
        Matrix matrix = new Matrix();
        matrix.setRotate(rotate);
        matrix.preTranslate(-(bitmapW / 2f), -(bitmapH / 2f));
        matrix.postTranslate((bitmapH / 2f), (bitmapW / 2f));
        List<Point> result = new ArrayList<>();
        for (Point point : srcArray) {
            result.add(matrix(matrix, point));
        }
        result.add(result.remove(0));
        return result.toArray(new Point[]{});
    }

    public static Point matrix(Matrix matrix, Point point) {
        float[] src = new float[]{point.x, point.y};
        float[] dst = new float[2];
        matrix.mapPoints(dst, src);
        return new Point((int) dst[0], (int) dst[1]);
    }

    public static Point[] createFullPoint(Bitmap bitmap) {
        return createFullPoint(bitmap.getWidth(), bitmap.getHeight());
    }

    public static Point[] createFullPoint(Document.Param param) {
        return createFullPoint(param.getWidth(), param.getHeight());
    }

    public static Point[] createFullPoint(int w, int h) {
        Point leftTop = new Point(0, 0);
        Point rightTop = new Point(w, 0);
        Point rightBottom = new Point(w, h);
        Point leftBottom = new Point(0, h);
        return new Point[]{leftTop, rightTop, rightBottom, leftBottom};
    }

    public static boolean isFullPoint(Bitmap bitmap, Point[] points) {
        if (bitmap == null) return false;
        if (bitmap.isRecycled()) return false;
        if (points == null) return false;
        if (points.length != 4) return false;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        return points[0].equals(0, 0) &&
            points[1].equals(width, 0) &&
            points[2].equals(width, height) &&
            points[3].equals(0, height);
    }

    public static boolean isFullPoint(Document.Param param, Point[] points) {
        if (points == null) return false;
        if (points.length != 4) return false;
        int width = param.getWidth();
        int height = param.getHeight();
        return points[0].equals(0, 0) &&
            points[1].equals(width, 0) &&
            points[2].equals(width, height) &&
            points[3].equals(0, height);
    }
}
